// Location of the AJAX script
var url_feed = 'rpc.php?fetch=';
var sort_tables_desc = false;
var tablesArray = null;

//** toggles table sort Order
function setTablesSort(){
    sort_tables_desc = !sort_tables_desc;   
    showTables()
}

//** generic function to load single col to tree
function loadTree(listObj,records){
    while (listObj.hasChildNodes()) { 
        listObj.removeChild( listObj.lastChild ); 
    }
    //var listObj = Id('tablesList');
    //var listHead = document.createEle
    var c = 0;
    for(var i in records){
        var listitem = document.createElement('listitem');
        listitem.setAttribute('label', records[i]);
        listObj.appendChild(listitem);    
        c++;
    }
    return c;
}

//** Loads tables from server
function loadTables(){
    setLoadingBar(true, 'Fetching tables....')
    sort_tables_desc = false;
    var tabs = Id('tableTabs');
    var url = 'rpc.php?fetch=tables&filter=' + tabs.selectedItem.label.toLowerCase();
    httpGet(url,setTablesArray);
    
}
function showTables(){
    setLoadingBar(true, 'Loading tables from server');
    //var treeObj = Id('tablesTreeChildren');
    var listObj = Id('tables-list');
    //var sortColT = Id('tableNameCol')
    loadTree(listObj,tablesArray);

    //if(!sort_tables_desc){
      //  loadTree(listObj,tablesArray);
        //sortColT.setAttribute('sortDirection',  'ascending');
    //}else{
      //  loadTree(listObj,tablesArray.reverse());
        //sortColT.setAttribute('sortDirection',  'descending');
    //}
    setLoadingBar(false)
}

function setTablesArray(stream){
   var data = eval(stream); 
   tablesArray = data.tables;
   Id('tables-count-stats-label').label = tablesArray.length + ' items';
   showTables()
}

//function loadCols(event){
function loadCols(obj){
    setLoadingBar(true,'Fetching columns...');
    var tableName = Id('tables-list').selectedItem.getAttribute('label');
    setValue('table-label',tableName);
    var url = 'rpc.php?fetch=cols&table=' + tableName;
    httpGet(url, showCols );
}

function showCols(stream){
    var data = eval(stream); 
    //alert(data);
    var colsList = Id('cols-list');
    cnt = loadTree(colsList,data.cols);
    //alert(cnt);
    //return;

    Id('cols-count-status-label').setAttribute('label', cnt + ' cols' );

    var tCols = Id('previewCols');
    while (tCols.hasChildNodes()) { 
        tCols.removeChild(tCols.lastChild); 
    }
    var c = 1;
    for(var i in data.cols){
        var tCol = document.createElement('treecol');
        tCol.setAttribute('label', data.cols[i]);
        tCols.appendChild(tCol);
        if(c < data.cols.length){ 
            var splitter = document.createElement('splitter');
            tCols.appendChild(splitter); 
        }   
        c++;        
    }
    preTree  = Id('previewTreeChildren');
    while (preTree.hasChildNodes()) { 
        preTree.removeChild( preTree.lastChild ); 
    }
    for(var x in data.preview){
        var tItem = document.createElement('treeitem');
        var tRow = document.createElement('treerow');
        tItem.appendChild(tRow);
        for(var c in data.preview[x]){
            var tCell = document.createElement('treecell');
            tCell.setAttribute('label',data.preview[x][c]);
            tRow.appendChild(tCell);
        }
        preTree.appendChild(tItem);
    }
    setLoadingBar(false);    
}

function setLoadingBar(state){
    Id('progressMeter').mode =  state ? 'undetermined' : 'determined';
    bar = Id('status-label');
    //alert(arguments.length);
    if(arguments.length > 1){
       bar.setAttribute('label',arguments[1]);
    }else{
        bar.setAttribute('label', '');
    }
    //bar.setAttribute('label',arguments.length );
}